<?php
// +----------------------------------------------------------------------
// | Cron控制器
// +----------------------------------------------------------------------
// | Since: 2014-09-15 16:02
// +----------------------------------------------------------------------
// | Author: shioujun <shioujun@gmail.com>
// +----------------------------------------------------------------------
namespace Crontab\Controller;

use Think\Controller;
use Com\Weixin\WeixinApi;
use Duobao\Model\DBConfModel as DBConf;
use Duobao\Controller\LogController as Mylog;

/**
 */
class TaskController extends Controller
{

    function gettoken()
    {
        $wx_obj = new WeixinApi();
        
        $access_token = $wx_obj->getToken( true );
        
        echo $access_token."\n\r";
        var_dump( S('access_token') );
        die;
        
        
        return false;
    }


    /**
     * 计算佣金
     */
    public function calculationCommission(){
        #Mylog::write('进入计算佣金的crontab');
        $model = M('member_go_record');
        $condition['time'] = array('gt','1454296907');
        $condition['handle_status'] = 0;
        #获取用户的uid在用户购买记录表中
        $result = $model->where($condition)->field('id,moneycount,uid')->limit(10)->order('id')->select();
        if(!$result){
            return false;
        }
        #获取用户的金额和uid，根据uid对上层分钱
        $relationship = M('partner_relationship');
        $model2 = new \Think\Model();
        $dbconf = new DBConf();

        #开启事务

        foreach($result as $value){
            $model2->startTrans();

            $condition2['uid'] = $value['uid'];
            $result2 = $relationship->where($condition2)->field('pid,gpid,ggpid')->find();

            #判断是否存在上级id，如果存在上级id，进行分钱，不存在上级id，就直接不分钱,设置处理状态为2表示没有上级元素
            if($result2){
                #计算核心佣金
                $h_com = $value['moneycount'] * $dbconf->getConfField('core_ratio');
                #更新核心合伙人佣金
                $query1 = $model2->execute("update __PREFIX__partner set `t_commission` = `t_commission`+$h_com,`s_commission`=`s_commission`+$h_com,`h_partner`=`h_partner`+$h_com where uid=".$result2['pid']);

                #更新高级合伙人佣金
                if($result2['gpid'] != 0){
                    $m_com = $value['moneycount'] * $dbconf->getConfField('adv_ratio');
                    $query2 = $model2->execute("update __PREFIX__partner set `t_commission` = `t_commission`+$m_com,`s_commission`=`s_commission`+$m_com,`m_partner`=`m_partner`+$m_com where uid=".$result2['gpid']);
                }else{
                    $query2 = true;
                }

                #跟新普通合伙人佣金
                if($result2['ggpid'] != 0){
                    $s_com = $value['moneycount'] * $dbconf->getConfField('general_ratio');
                    $query3 = $model2->execute("update __PREFIX__partner set `t_commission` = `t_commission`+$s_com,`s_commission`=`s_commission`+$s_com,`s_partner`=`s_partner`+$s_com where uid=".$result2['ggpid']);
                }else{
                    $query3 = true;
                }

                $query4 = $model2->execute("update __PREFIX__member_go_record set `handle_status` = 1 WHERE `id` = ".$value['id']);

                if($query1 && $query2 && $query3 && $query4){
                    $model2->commit();
                    #Mylog::write('事务成功');
                }else{
                    $model2->rollback();
                    Mylog::write('计算佣金事务失败回滚');
                }
            }else{
                $query5 = $model2->execute("update __PREFIX__member_go_record set `handle_status` = 2 WHERE `id` = ".$value['id']);
                if(!$query5){
                    Mylog::write($value['uid'].'号用户结算异常');
                }
            }
        }
    }
}